package com.chen.auth.bestmanage.config;

import com.alibaba.fastjson.JSON;
import com.chen.auth.bestmanage.utils.JsonResult;
import com.chen.auth.bestmanage.utils.ResultCode;
import com.chen.auth.bestmanage.utils.ResultTools;
import org.springframework.security.web.session.SessionInformationExpiredEvent;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
 ClassName:CustomizeSessionInformationExpiredStrategy
 Package:com.chen.auth.bestmanage.config
 Description:会话管理策略
 @date:2021/9/19 下午2:28
  * @author:陈国松
 */
@Component
public class CustomizeSessionInformationExpiredStrategy implements SessionInformationExpiredStrategy {
    @Override
    public void onExpiredSessionDetected(SessionInformationExpiredEvent sessionInformationExpiredEvent) throws IOException, ServletException {
        JsonResult result = ResultTools.fail(ResultCode.USER_ACCOUNT_USE_BY_OTHERS);
        HttpServletResponse httpServletResponse = sessionInformationExpiredEvent.getResponse();
        httpServletResponse.setContentType("text/json;charset=utf-8");
        httpServletResponse.getWriter().write(JSON.toJSONString(result));
    }
}
